/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BD;

import BE.Mesa;
import BE.Cliente;
import java.util.Collection;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Xatuuu
 */
public class MesaBD {

    private Session session = null;

    public MesaBD() {

        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public Collection buscarMesaenMesa(Mesa obMesa) {
        List<Mesa> lista = null;
        try {
            Transaction tx = session.beginTransaction();
            Criteria crit = session.createCriteria(Mesa.class);

            if ( obMesa.getSrlIdmesa() > 0) {
                crit.add(Restrictions.eq("srlIdmesa", obMesa.getSrlIdmesa()));
            }
            if (obMesa.getCvNombre() != "") {
                crit.add(Restrictions.like("cvNombre","%"+obMesa.getCvNombre()+"%"));
                }
             crit.add(Restrictions.eq("boolEliminado", false));
            session.flush();
            lista = crit.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lista;
    }

    public boolean insertarMesa(Mesa objMesa) {

        try {

            Transaction tx = session.beginTransaction();
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = (Transaction) session.beginTransaction();
            session.save(objMesa);
            tx.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

    public Collection seleccionarMesa() {


        Transaction tx = session.beginTransaction();
        Criteria crit = session.createCriteria(Mesa.class);
        return crit.list();
    }
}
